package com.zx.util;

import com.google.common.base.Preconditions;
import com.zx.account.entity.User;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebListener
public class SessionDataUtil implements ServletRequestListener {

    private static final ThreadLocal<HttpSession> sessionLocal = new ThreadLocal<>();

    //	private static final String[] INHERENT_ESCAPE_URIS = {".jpg", ".js", ".css", ".gif", ".png", ".html", ".ico"};
    private static final String[] INHERENT_ESCAPE_URIS = {};

    private static HttpSession getHttpSession() {
        return sessionLocal.get();
    }

    public static User getUserInfo() {
        HttpSession session = getHttpSession();
        if (session == null) {
            return null;
        }
        Object attribute = session.getAttribute(Constants.HTTP_SESSION_USER_INFO_KEY);
        return attribute == null ? null : (User) attribute;
    }

    public static void setUserInfo(User user) {
        HttpSession session = getHttpSession();
        if (session == null) {
            return;
        }
        Preconditions.checkNotNull(user);
        user.setPassword("");
        session.setAttribute(Constants.HTTP_SESSION_USER_INFO_KEY, user);
    }

    public static void rmUserInfo() {
        HttpSession session = getHttpSession();
        if (session == null) {
            return;
        }
        session.removeAttribute(Constants.HTTP_SESSION_USER_INFO_KEY);
    }

    public static String getVerification() {
        HttpSession session = getHttpSession();
        if (session == null) {
            return null;
        }
        Object attribute = session.getAttribute(Constants.HTTP_SESSION_VERIFICATION_KEY);
        return attribute == null ? null : (String) attribute;
    }

    public static void setVerification(String verification) {
        HttpSession session = getHttpSession();
        if (session == null) {
            return;
        }
        Preconditions.checkNotNull(verification);
        session.setAttribute(Constants.HTTP_SESSION_VERIFICATION_KEY, verification);
    }

    public static void rmVerification() {
        HttpSession session = getHttpSession();
        if (session == null) {
            return;
        }
        session.removeAttribute(Constants.HTTP_SESSION_VERIFICATION_KEY);
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        if (log.isDebugEnabled()) {
            log.debug("requestDestroyed#url={}", request.getRequestURI());
        }
        sessionLocal.remove();
    }

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
        if (log.isDebugEnabled()) {
            log.debug("requestInitialized#url={}", request.getRequestURI());
        }
        if (!this.isURILogin(request.getRequestURI())) {
            sessionLocal.set(request.getSession());
        }
    }

    /**
     * 当前URI资源是否需要登录才能访问
     *
     * @param requestURI
     * @param request
     * @return
     */
    private boolean isURILogin(String requestURI) {
        for (String uri : INHERENT_ESCAPE_URIS) {
            if (requestURI != null && requestURI.toLowerCase().endsWith(uri)) {
                return true;
            }
        }
        return false;
    }
}
